package com.math3d;

/**
 * @Author: clm
 * @Date: 2018-10-15
 */

public class Vector3f {
    public float x, y, z;
    public Vector3f(){
        x = y = z = 0;
    }
    public Vector3f(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getZ() {
        return z;
    }

    public void setZ(float z) {
        this.z = z;
    }
    public Vector3f cross(Vector3f tag){
        Vector3f vector3f = new Vector3f(y * tag.z - z * tag.y, z * tag.x - x * tag.z, x * tag.y - y * tag.x);
        return vector3f;
    }
    public Vector3f sub(Vector3f tag){
        return new Vector3f(x - tag.x, y - tag.y, z - tag.z);
    }
    public float dot(Vector3f tag){
        return x * tag.x + y * tag.y + z * tag.z;
    }
    public final void normalize(){
        double length = x * x + y * y + z * z;
        if(length > 0){
            //归一化
            length = 1 / Math.sqrt(length);
            x *= length;
            y *= length;
            z *= length;
        }
    }
    @Override
    public String toString() {
        return "Vector3f{" +
                "x=" + x +
                ", y=" + y +
                ", z=" + z +
                '}';
    }
}
